package com.ingrun.tuboshu.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.ingrun.tuboshu.entity.SysAdmin;
import com.ingrun.tuboshu.entity.vo.PasswordLoginQ;
import com.ingrun.tuboshu.entity.vo.UserListQ;
import com.ingrun.tuboshu.entity.vo.UpdPasswordQ;
import com.ingrun.tuboshu.service.ISysAdminService;
import com.ingrun.tuboshu.utils.Response;
import com.ingrun.tuboshu.utils.StpKit;
import com.ingrun.tuboshu.utils.params.PageHelperList;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 系统管理用户表	 前端控制器
 * </p>
 *
 * @author ingrun
 * @since 2025-01-23
 */
@Tag(name = "系统管理用户表")
@RestController
@RequestMapping("/sysAdmin")
public class SysAdminController {

    @Autowired
    private ISysAdminService sysAdminService;

    // 注册
    @PostMapping("/register")
    @Operation(summary = "注册管理员")
    @SaCheckLogin(type = "admin")
    public Response<Boolean> register(@RequestBody SysAdmin sysAdmin) {
        return Response.success(sysAdminService.register(sysAdmin));
    }

    @PostMapping("/login")
    @Operation(summary = "登录")
    public Response<String> login(@RequestBody PasswordLoginQ query) {
        return Response.success(sysAdminService.login(query));
    }

    @PostMapping("/logout")
    @Operation(summary = "退出登录")
    public Response<Boolean> logout() {
        StpKit.ADMIN.logout();
        return Response.success(true);
    }

    // 修改密码
    @PostMapping("/updatePassword")
    @Operation(summary = "修改密码")
    @SaCheckLogin(type = "admin")
    public Response<Boolean> updatePassword(@RequestBody UpdPasswordQ query) {
        return Response.success(sysAdminService.updatePassword(query));
    }

    // 获取当前登录用户信息
    @GetMapping("/getLonginUser")
    @Operation(summary = "获取当前登录用户信息")
    @SaCheckLogin(type = "admin")
    public Response<SysAdmin> getLonginUser() {
        return Response.success(sysAdminService.getLonginUser());
    }

    // 用户列表
    @GetMapping("/userList")
    @Operation(summary = "用户列表")
    @SaCheckLogin(type = "admin")
    public Response<PageHelperList<SysAdmin>> userList(@ModelAttribute UserListQ query) {
        return Response.success(sysAdminService.userList(query));
    }

}
